package com.atXiao.food.controller;

import com.atXiao.food.aspect.annotation.LogAspect;
import com.atXiao.food.config.Log;
import com.atXiao.food.constant.BusSignConstant;
import com.atXiao.food.constant.BuscodeConstant;
import com.atXiao.food.reqbody.ReqBody;
import com.atXiao.food.reqbody.history.HistoryBroseReq;
import com.atXiao.food.reqbody.receipt.SearchIconReq;
import com.atXiao.food.rspbody.RspBody;
import com.atXiao.food.rspbody.history.UserHistory;
import com.atXiao.food.rspbody.recipe.ReceiptIcon;
import com.atXiao.food.rspcode.RspResultCode;
import com.atXiao.food.service.UserHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author: lixiaoxiao
 * @date: 2023/11/24 11:28
 * @version: 1.0
 * @desc:
 */
@RestController
@RequestMapping(path = "/history", produces = MediaType.APPLICATION_JSON_VALUE)
public class UserHistoryController {

    public final static Log logger = new Log(UserHistoryController.class);

    @Autowired
    UserHistoryService userHistoryService;


    /**
     * 用户历史记录查询
     *
     * @param trace
     * @param reqBody
     * @return
     */
    @PostMapping(path = "/search")
    @ResponseBody
    @LogAspect(busSign = BusSignConstant.TASTE_FOOD, busCode = BuscodeConstant.HISTORY_SEARCH)
    public RspBody<List<UserHistory>> historyBrose(@RequestHeader(name = "Trace") String trace, @Valid @RequestBody ReqBody<HistoryBroseReq> reqBody) {
        RspBody<List<UserHistory>> rspBody = new RspBody();
        try {
            List<UserHistory> historyList = userHistoryService.queryUserHistory(trace, reqBody.getReqBody());
            rspBody.setRetCode(RspResultCode.REQUEST_SUCCESS.getCode());
            rspBody.setRetDesc(RspResultCode.REQUEST_SUCCESS.getDesc());
            rspBody.setRspBody(historyList);
        } catch (Exception e) {
            logger.error(trace, BusSignConstant.TASTE_FOOD, BuscodeConstant.HISTORY_SEARCH, e.getMessage(), e);
            rspBody = RspBody.fail(null);
        }
        return rspBody;
    }


}
